//
//  AppDelegate.swift
//  MyColorPicker
//
//  Created by 火星人 on 2018/9/18.
//  Copyright © 2018年 火星人. All rights reserved.
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    
    var mainWindow: NSWindowController?
    var editCodeWindow: NSWindowController?
    var pickingColor = false
    
    var hkShowMainWindow: HotKey?
    
    var statusItem: NSStatusItem?
 
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        
        if userDefaults.string(forKey: "showStatusBarIcon") == "showStatusBarIcon" {
            showStatusIcon()
        }
       
        self.initUserDefaults()
        updateHotKey()
    }
    
    func showStatusIcon(){
        statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
        statusItem?.image = NSImage(named: "status_icon")
        statusItem?.action = #selector(self.showMainWindow)
    }
    
    func hideStatusIcon(){
        if let statusItem = statusItem {
            NSStatusBar.system.removeStatusItem(statusItem)
        }
    }
    
    @objc private func showMainWindow(){
        NSApp.activate(ignoringOtherApps: true)
        mainWindow?.showWindow(nil)
    }
    
    private func initUserDefaults(){
        if userDefaults.value(forKey: "codeFormat0") == nil {
            userDefaults.set([
                "NSColor(red: %.2f, green: %.2f, blue: %.2f, alpha: 1.00)",
                "r",
                "g",
                "b"
                ], forKey: "codeFormat0")
            userDefaults.set([
                "rgb(%d, %d, %d)",
                "R",
                "G",
                "B"
                ], forKey: "codeFormat1")
            userDefaults.set([
                "cc.hexToColor('%x%x%x')",
                "R",
                "G",
                "B"
                ], forKey: "codeFormat2")
            userDefaults.set("showStatusBarIcon", forKey: "showStatusBarIcon")
        }
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
        
        if let statusItem = statusItem {
            NSStatusBar.system.removeStatusItem(statusItem)
        }
    }
    
    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        if !flag {
            mainWindow?.showWindow(nil)
        }
        return true
    }
    
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        if pickingColor {
            return false
        }
        if userDefaults.string(forKey: "closeQuit") == "closeQuit" {
            return true
        }
        else {
            return false
        }
    }
    
    func updateHotKey(){
        
        if let key = userDefaults.string(forKey: "showMainWindow") {
            if let k = Key(string: key) {
                weak var wself = self
                self.hkShowMainWindow = HotKey(key: k, modifiers: [.command, .shift], keyDownHandler: nil, keyUpHandler: {
                    NSApp.activate(ignoringOtherApps: true)
                    wself?.mainWindow?.showWindow(nil)
                })
                return
            }
        }
        self.hkShowMainWindow = nil
        
    }

}

